<?php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\goods;
use Illuminate\Support\Str;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class Admin extends Controller
{
    function admin(Request $request)
    {
        $value = $request->session()->get('username');
        if(!isset($value)){
            return redirect("adminlogin");
        }
        return view('admin',['name' => $value]);
    }
    //显示控制器页面
    function console()
    {
        $user_row = DB::table('user')->get();
        $goods_row = DB::table('goods')->get();

        $user_number = count($user_row);
        $goods_number = count($goods_row);

        return view('console',['user_number' => $user_number,'goods_number' => $goods_number]);
    }
    //显示商品管理页面
    function goods()
    {
        $goods = DB::table('goods')->get()
            ->map(function ($value) {return (array)$value;})
            ->toArray();

            return view('goods');
    }
    //显示商品管理添加页面
    function goods_add()
    {
        return view('goods_add');
    }
    //添加商品
    function goods_ok(Request $request)
    {
        $row = $request->input();

        $file = $request->file('img');

        $folder_name = "uploads/images/avatars/" . date("Ym/d", time());//获取路径加上时间
        $upload_path = public_path() . '/' . $folder_name;//获取完整路径
        $extension  =  strtolower($file->getClientOriginalExtension())  ?:  'png';// 获取文件的后缀名
        $filename =  time() . '_' . Str::random(10) . '.' . $extension;//加前缀,为了增加辨析度
        $path = $folder_name . '/' . $filename;//获取存入数据库里的路径
        $create_time = Carbon::now()->toDateTimeString();//获取当前时间

        if($file->move($upload_path, $filename)){
            goods::insert([
                [
                    'name' => $row['name'],
                    'detail' => $row['detail'],
                    'Subtitle' => $row['Subtitle'],
                    'price' => $row['price'],
                    'img' => $path,
                    'inventory' => $row['inventory'],
                    'issueNumber' => $row['issueNumber'],
                    'need' => $row['need'],
                    'created_at' => $create_time,
                    'updated_at' => $create_time,
                    'Display' => $row['Display']
                ]
            ]);
            return redirect("goods");
        }

    }
    //显示用户管理页面
    function users(Request $request)
    {
        $data = $request->input();//接收搜索表单数据然后进行模糊查询

        return view('users');
    }
    //显示用户添加页面
    function users_add()
    {
        return view('users_add');
    }
    //显示用户添加页面
    function users_ok(Request $request)
    {
        $row = $request->input();

        $file = $request->file('userPhoto');

        $folder_name = "user_uploads/user_images/avatars/" . date("Ym/d", time());//获取路径加上时间
        $upload_path = public_path() . '/' . $folder_name;//获取完整路径
        $extension  =  strtolower($file->getClientOriginalExtension())  ?:  'png';// 获取文件的后缀名
        $filename =  time() . '_' . Str::random(10) . '.' . $extension;//加前缀,为了增加辨析度
        $path = $folder_name . '/' . $filename;//获取存入数据库里的路径
        $create_time = Carbon::now()->toDateTimeString();//获取当前时间
       dd($extension);
    }
}

